package com.mrobuy.jd.product;

import com.gavel.database.SQLExecutor;
import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.MyProductInfoDto;
import com.jd.open.api.sdk.request.supplier.VcItemProductsFindRequest;
import com.jd.open.api.sdk.response.supplier.VcItemProductsFindResponse;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jdvc.entity.VCCateBrand;
import com.mrobuy.jdvc.entity.VCCategory;
import com.mrobuy.jdvc.entity.VCProductInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * 获取vc后台审核通过的商品列表
 *
 * jingdong.vc.item.products.find ( 获取我的商品列表 )
 *
 */
public class VcItemProductsFind {

    private static final int PAGE_SIZE = 50;

    public static enum SaleState {

        State_0(0, "下柜"),
        State_1(1, "上柜"),
        State_2(2, "可上柜");

        private int code;
        private String title;

        SaleState(int code, String title) {
            this.code = code;
            this.title = title;
        }

        public int code() {
            return code;
        }

        public String title() {
            return title;
        }
    }

    public static void main(String[] args) throws Exception {

        find(JdClientFactory.jdClient());


    }

    private static void find(JdClient client) throws Exception {


        List<VCCateBrand> vcCateBrands = SQLExecutor.executeQueryBeanList("select  * from VC_CATE_BRAND ", VCCateBrand.class);

        List<MyProductInfoDto> productInfoDtos = new ArrayList<>();

        for (VCCateBrand cateBrand : vcCateBrands) {
            try {
                productInfoDtos.addAll(load(cateBrand.getBrandid(),cateBrand.getCateid(), client));
                // productInfoDtos.addAll(load(548836, 14127, client));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }



        for (int i = 0; i < productInfoDtos.size(); i++) {
            MyProductInfoDto productInfoDto = productInfoDtos.get(i);
            System.out.println((i+1) + ". " + new Gson().toJson(productInfoDtos.get(i)));


            VCProductInfo productInfo = null;
            try {
                productInfo =  SQLExecutor.executeQueryBean("select * from VC_PRODUCT_INFO where WAREID = ? ", VCProductInfo.class, productInfoDto.getWareId());
            } catch (Exception e) {
                e.printStackTrace();
            }

            boolean insert =  ( productInfo==null);
            if ( insert ) {
                productInfo = new VCProductInfo();
            }

            productInfo.setWareId(productInfoDto.getWareId());
            productInfo.setWareName(productInfoDto.getWareName());
            productInfo.setSaleState(productInfoDto.getSaleState()); // 2: 可上柜; 1: 上柜; 0: 下柜
            productInfo.setIsPrimary(productInfoDto.getIsPrimary());
            productInfo.setIsGaea(productInfoDto.getIsGaea());
            productInfo.setModifyTime(productInfoDto.getModifyTime());
            productInfo.setUpdateTime(Calendar.getInstance().getTime());
            productInfo.setVendorCode("wysgyp");

            if ( insert ) {
               SQLExecutor.insert(productInfo);
            } else {
                SQLExecutor.update(productInfo);
            }
        }

        System.out.println("Total: " + productInfoDtos.size());
    }


    private static List<MyProductInfoDto> load(Integer brandId, Integer categoryId, JdClient client) throws Exception {

        System.out.println("brandId: " + brandId + "; categoryId: " + categoryId);

        if ( brandId==null || categoryId==null || client==null ) {
            return Collections.EMPTY_LIST;
        }

        List<MyProductInfoDto> productInfoDtos = new ArrayList<>();

        int offset = 0;

        VcItemProductsFindRequest request=new VcItemProductsFindRequest();

        request.setBrandId(brandId);
        request.setCategoryId(categoryId); //
        request.setOffset(offset);
        request.setPageSize(PAGE_SIZE);

        VcItemProductsFindResponse response = client.execute(request);
        if ( response==null ) {
            throw new RuntimeException("API调用异常: response is null!");
        }

        if ( response.getJosResultDto()==null ) {
            throw new RuntimeException("API调用异常: response.getJosResultDto() is null!");
        }

        if ( !response.getJosResultDto().getIsSuccess() ) {
            throw new RuntimeException(response.getJosResultDto().getReturnMessage());
        }

        int cnt = response.getJosResultDto().getCount();

        productInfoDtos.addAll(response.getJosResultDto().getResult());


        while ( productInfoDtos.size() < cnt ) {
            offset += response.getJosResultDto().getResult().size();
            request.setOffset(offset);

            response = client.execute(request);
            if ( response==null ) {
                throw new RuntimeException("API调用异常: response is null!");
            }

            if ( response.getJosResultDto()==null ) {
                throw new RuntimeException("API调用异常: response.getJosResultDto() is null!");
            }

            if ( !response.getJosResultDto().getIsSuccess() ) {
                throw new RuntimeException(response.getJosResultDto().getReturnMessage());
            }

            productInfoDtos.addAll(response.getJosResultDto().getResult());
        }


        return productInfoDtos;
    }



}
